package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.BaseSourceState2;
import beans.HttpConfig;
import beans.HttpResult;
import dao.JobDao;
import engine.BIEngine0_1.AlgorithmOperator;
import net.HttpRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class PlatformResourceServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public String token;
	

	
	   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        doPost(request, response);
	    }
	   
	   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		   String taskId = request.getParameter("taskId");
		   
		   if(taskId!=null && taskId!=""){
			   //根据taskHash查询计算节点的资源情况
			  // List<Object> list = new JobDao().getJobByTaskhash(taskId);
			   
				
			 
			   
			   
		   }else{
			   //查询平台所有节点的资源情况
			   
			   /**
				 * 从服务器获取token
				 */
				HttpResult tokenResult = AlgorithmOperator.getToken(HttpConfig.userName, HttpConfig.passWord);
				if(tokenResult.isResult()){
					this.token = tokenResult.getMessage();
					 JSONObject result = JSONObject.fromObject(HttpRequest.sendGet(HttpConfig.getBaseResourceStateUrl, "token=" + this.token));
					 if (result.getString("result").equals("success")) {
						 
						 JSONArray data = result.getJSONArray("data");
			                List<Object> states = new ArrayList<>();
			                for (int i = 0; i < data.size(); i++) {
			              //      BaseResourceState state = new BaseResourceState();
			                	BaseSourceState2 state = new BaseSourceState2();
			                    JSONObject oneData = data.getJSONObject(i);
			                    state.setHostName(oneData.getString("HOST_NAME"));
			                    state.setStatus(oneData.getString("status"));
			                    state.setR15s(oneData.getString("r15s"));
			                    state.setR1m(oneData.getString("r1m"));
			                    state.setR15m(oneData.getString("r15m"));
			                    state.setUt(oneData.getString("ut"));
//			                    state.setPg(oneData.getString("pg"));
			                    state.setLs(oneData.getString("ls"));
//			                    state.setIt(oneData.getString("it"));
//			                    state.setTmp(oneData.getString("tmp"));
//			                    state.setSwp(oneData.getString("swp"));
			                    state.setMem(oneData.getString("mem"));
			                    state.setType(oneData.getString("type"));
			                    states.add(state);
			                }
			                JSONObject json = new JSONObject();
			                json.put("resource", states);
			                response.getWriter().print(json.toString());
					 }
				}
			   
		   }
		  
		   
	    }
	
}
